import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1979. 找出数组的最大公约数
 * https://leetcode-cn.com/problems/find-greatest-common-divisor-of-array/
 */
public class Solutions_1979 {
    public static void main(String[] args) {
        int[] nums1 = {2, 5, 6, 9, 10};  // output: 2
        int[] nums2 = {7, 5, 6, 8, 3};  // output: 1
        int[] nums3 = {3, 3};  // output: 3

        List<int[]> inputs = new ArrayList<>();
        inputs.add(nums1);
        inputs.add(nums2);
        inputs.add(nums3);

        for (int[] input : inputs) {
            int result = findGCD(input);
            System.out.println(result);
        }
    }

    public static int findGCD(int[] nums) {
        Arrays.sort(nums);
        int a = nums[0], b = nums[nums.length - 1];
        int res = gcd(a, b);
        return res;
    }

    /**
     * 辗转相除法
     * @param a
     * @param b
     * @return
     */
    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }
}
